﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using GWEB.WebBase.Data;
using GWEB.WebBase.Entity;
using GWEB.WebBase.Utility;
using GWEB.WebFrame.Public;
using Newtonsoft.Json;

namespace GWEB.WebFrame.MealCourses
{
    public class MealCourseController : PageInfoBase
    {
        [ActionFilter(IsLogin = false)]
        public ActionResult Index(String id, String keyCode)
        {
            return Load("MealCourse", "MealCourses/MealCourse", id, keyCode);
        }
        [ActionFilter(IsLogin = false)]
        public JsonResult Data(String type, String id, String keyCode)
        {
            PageInfo pg = isBrowse(type, id, keyCode);
            if (pg == null)
            {
                return BasePage.Json(ResultData.Error(WebMessage.NoBrowse));
            }
            String sql = @"select id,CustName,ChildName,CustTel,LinkName,LinkRelation,LatelyReceId,Sex,cusImage,isnull(address,'未设置') address, 
                   isnull((select SUM(account) je from cu_Account where cid=cu_Customers.id  and FlowingType in ('00101001','00201001')   and state=2),0) je,
isnull((select CreateUser.sshowname from  tb_sys_users CreateUser where CreateUser.iuserid  = cu_Customers.LatelyReceId),'未设置') AchievementName
                 from cu_Customers where CustTel like '%" + keyCode + "%' and LeagueId='1' ";
            BasePage.bd.ColumnNameToLower = false;
            String str = JsonConvert.SerializeObject(BasePage.bd.ExecuteTable(sql));
            BasePage.bd.ColumnNameToLower = true;
            List<Dictionary<String, Object>> data = JsonConvert.DeserializeObject<List<Dictionary<String, Object>>>(str);
            return BasePage.Json(ResultData.OK(data));

        }
        [ActionFilter(IsLogin = true)]
        public JsonResult CourseData()
        {
            //String type, String id, String keyCode
            //PageInfo pg = isBrowse(type, id, keyCode);
            //if (pg == null)
            //{
            //    return BasePage.Json(ResultData.Error(WebMessage.NoBrowse));
            //}
            String sql = "select * from cu_sys_course where  LeagueId='1' ";
            BasePage.bd.ColumnNameToLower = false;
            String str = JsonConvert.SerializeObject(BasePage.bd.ExecuteTable(sql));
            BasePage.bd.ColumnNameToLower = true;
            List<Dictionary<String, Object>> data = JsonConvert.DeserializeObject<List<Dictionary<String, Object>>>(str);
            return BasePage.Json(ResultData.OK(data));
        }



        [ActionFilter(IsLogin = true)]
        public JsonResult HtData(String type, String id, String keyCode)
        {
            PageInfo pg = isBrowse(type, id, keyCode);
            if (pg == null)
            {
                return BasePage.Json(ResultData.Error(WebMessage.NoBrowse));
            }
            String sql = "select cu_Contract.id,contractNumber,SetmealName,ContractPrice,DisPrice,CustName,ChildName,CustTel,LinkName,LatelyReceId,cu_Contract.cid from cu_Contract left join  cu_Customers on cu_Customers.id=cu_Contract.cid where cu_Contract.state=2 and contractNumber like '%" + keyCode + "%' and cu_Contract.LeagueId='1' ";
            BasePage.bd.ColumnNameToLower = false;
            String str = JsonConvert.SerializeObject(BasePage.bd.ExecuteTable(sql));
            BasePage.bd.ColumnNameToLower = true;
            List<Dictionary<String, Object>> data = JsonConvert.DeserializeObject<List<Dictionary<String, Object>>>(str);
            return BasePage.Json(ResultData.OK(data));

        }

        [ActionFilter(IsLogin = true)]
        public JsonResult FlowingTypeData(String type, String id, String keyCode)
        {
            //PageInfo pg = isBrowse(type, id, keyCode);
            //if (pg == null)
            //{
            //    return BasePage.Json(ResultData.Error(WebMessage.NoBrowse));
            //}
            string Lx = "";
            if (keyCode == "收入类")
                Lx = "001";
            if (keyCode == "支出类")
                Lx = "002";
            if (keyCode == "预算")
                Lx = "003";
            String sql = "select did,dname,typeid from tb_sys_dictionary where typeid='D15' and  did like '" + Lx + "%' ";
            BasePage.bd.ColumnNameToLower = false;
            String str = JsonConvert.SerializeObject(BasePage.bd.ExecuteTable(sql));
            BasePage.bd.ColumnNameToLower = true;
            List<Dictionary<String, Object>> data = JsonConvert.DeserializeObject<List<Dictionary<String, Object>>>(str);
            return BasePage.Json(ResultData.OK(data));

        }

        [ActionFilter(IsLogin = false)]
        public JsonResult UpdateState(String id, String keyCode, String state)
        {
            if (!Fun.IDVerify(id, keyCode))
                return BasePage.Json(ResultData.Code("0001", "您没有权限"));

            BaseData bd = new BaseData();
            string UserId = BasePage.GetUserId(Session);
            String sql = "update cu_Sys_contract set state={0},auditingid='" + UserId + "',auditingdate='" + System.DateTime.Now.ToString() + "' where id={1}";


            bool t = bd.ExecuteSQL(string.Format(sql, state, id));
            if (t)
                return BasePage.Json(ResultData.Code("0000", WebMessage.SaveSuccess));
            else
                return BasePage.Json(ResultData.Code("0001", WebMessage.SaveFailure));
        }
        public JsonResult DelCourse(String id, String keyCode)
        {
            if (!Fun.IDVerify(id, keyCode))
                return BasePage.Json(ResultData.Code("0001", "您没有权限"));

            BaseData bd = new BaseData();
            string UserId = BasePage.GetUserId(Session);
            String sql = "delete cu_Sys_contract where id={0}";


            bool t = bd.ExecuteSQL(string.Format(sql, id));
            if (t)
                return BasePage.Json(ResultData.Code("0000", WebMessage.SaveSuccess));
            else
                return BasePage.Json(ResultData.Code("0001", WebMessage.SaveFailure));
        }

        protected override void FieldAddInit(DataRow data)
        {
            //data["CreateId"] = BasePage.GetUserId(Session);
            data["LeagueId"] = 1;
            ViewBag.Lx = 0;
            //data["state"] = "0";
            data["IsAll"] = 0;
            data["IsClassHour"] = 0;
            data["Createdate"] = DateTime.Now.ToString();
            base.FieldAddInit(data);
        }
        protected override void FieldEditInit(DataRow data)
        {
            ViewBag.Lx = 1;
            base.FieldEditInit(data);
        }

    }
}